package com.wmm.controller;

import com.wmm.domain.*;
import com.wmm.service.MenuService;
import com.wmm.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;


    /**
     * 查询所有角色&条件进行查询
     */

    @RequestMapping("/findAllRole")
    public ResponseResult findAllRole(@RequestBody Role role) {
        List<Role> allRole = roleService.findAllRole(role);
        ResponseResult responseResult = new ResponseResult(true, 200, "查询所有角色成功", allRole);
        return responseResult;
    }


    /**
     * 查询所有的父子菜单信息
     */

    @Autowired
    private MenuService menuService;

    @RequestMapping("/findAllMenu")
    public ResponseResult findSubMenuListByPid(Integer pid) {

        //-1 表示查询所有的父子级菜单
        List<Menu> menuList = menuService.findSubMenuListByPid(-1);

        Map<String,Object> map = new HashMap<String,Object>();
        map.put("parentMenuList",menuList);
        ResponseResult responseResult = new ResponseResult(true,200,"查询所有的父子菜单信息成功",map);
        return responseResult;
    }

    /**
     * 根据角色ID查询该角色关联的菜单信息ID
     */
    @RequestMapping("/findMenuByRoleId")
    public ResponseResult findMenuByRoleId(Integer roleId) {
        List<Integer> menuByRoleId = roleService.findMenuByRoleId(roleId);
        ResponseResult responseResult = new ResponseResult(true, 200, "查询角色关联的菜单信息", menuByRoleId);
        return responseResult;
    }

    /**
     * 为角色分配菜单
     */
    @RequestMapping("/RoleContextMenu")
    public ResponseResult RoleContextMenu(@RequestBody RoleMenuVo roleMenuVo) {

        roleService.roleContextMenu(roleMenuVo);
        return new ResponseResult(true,200,"响应成功",null);

    }

    /**
     * 删除角色
     *
     */
     @RequestMapping("/deleteRole")
     public ResponseResult deleteRole(Integer id) {
         roleService.deleteRole(id);
         ResponseResult responseResult = new ResponseResult(true,200,"删除角色成功",null);
         return responseResult;
     }

    /**
     * 名称: findResourceListByRoleId
     * 描述: 获取当前角色具有资源
     * URL: http://localhost:8080/ssm_web/role/findResourceListByRoleId
     * 请求方式: GET
     * @param roleId
     * @return
     */
    @RequestMapping("/findResourceListByRoleId")
    public ResponseResult findResourceListByRoleId(int roleId) {
        ResourceCategoryVO resourceCategoryVO = new ResourceCategoryVO();
         //根据角色ID查询出来的所具有的资源分类的Id
        List<Integer> resourceCategoryByRoleId = roleService.findResourceCategoryByRoleId(roleId);
        //声明资源集合
        List<ResourceCategory> list = new ArrayList<>();

        for(Integer id : resourceCategoryByRoleId) {
            //查询资源分类对象
            ResourceCategory resourceCategory = roleService.findResourceCategoryById(id);

            //设置资源分类id
            resourceCategoryVO.setId(id);
            //查询当前角色拥有资源id
            resourceCategoryVO.setList(roleService.findResourceByRoleId(roleId));
            //该资源分类id下当前角色具有的资源
            List<Resource> resourceList = roleService.findResourceByResourceCategoryId(resourceCategoryVO);
            resourceCategory.setResourceList(resourceList);
            list.add(resourceCategory);
        }
        return new ResponseResult(true,200,"获取当前角色拥有的资源信息成功",list);
    }

    /**
     * 名称: roleContextResource
     * 描述: 添加 & 修改资源分类接口
     * URL: http://localhost:8080/ssm_web/role/roleContextResource
     * 请求方式: POST
     * @return
     */
    @RequestMapping("/roleContextResource")
    public ResponseResult findResourceListByRoleId(@RequestBody RoleResourceVo roleResourceVo) {

        roleService.roleContextResource(roleResourceVo);
        return new ResponseResult(true,200,"为角色分配资源成功",null);

    }


}
